package com.lg.erp.controller.zhanghuan;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.zhanghuan.TSettlement;
import com.lg.erp.entity.zhanghuan.TVehicle;
import com.lg.erp.service.zhanghuan.TSettlementService;
import com.lg.erp.util.EmptyChecker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/***
 *
 * 物流结算
 * */
@RestController
public class TSettlementController {

    @Autowired
    private TSettlementService tSettlementService;

    @GetMapping("/settlements")
    public JsonResult pageSettlement(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,String shippingNum) {
        shippingNum = "%" + shippingNum + "%";
        Page<TSettlement> page = new Page<>(current, 5);
        IPage<TSettlement> settlementIPage = tSettlementService.pageSettlement(page,shippingNum);
        return new JsonResult(1, "ok", settlementIPage);
    }

    @PostMapping("/settlement")
    public JsonResult updateSettlement(@RequestBody TSettlement tSettlement) {
        //判断是否存在
        boolean result = EmptyChecker.isEmpty(tSettlementService.selectByShippingNum(tSettlement.getShippingNum()));
        TSettlement settlement = new TSettlement();
        if (result) {
            String id = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
            settlement.setSettlementId(id);
            settlement.setShippingNum(tSettlement.getShippingNum());
            settlement.setSettlementStatus("1");
            settlement.setSettlementMoney(tSettlement.getSettlementMoney());
            tSettlementService.save(settlement);
        }else{
            settlement.setSettlementStatus("1");
            settlement.setSettlementMoney(tSettlement.getSettlementMoney());
            tSettlementService.update(settlement,new QueryWrapper<TSettlement>().eq("shipping_num", tSettlement.getShippingNum()));
        }
        return new JsonResult(1, "ok", null);
    }

}
